var vm = new Vue({ 
    el: '#container' 
    , data: {
        buttons: {}
        , query: {
            page: 1
            , pageSize: hcCore.pageSize
            , codeName: ''
        }
        , tableData: {
            records: []
            , total: 0
        }
        , edit: {
            formId: 'editFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '编号'
                , name: 'pkid'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入编号'
                , rules: {
                    required: true, message: '编号不能为空', trigger: 'blur'
                }
            }, {
                label: '编号名称'
                , name: 'code_name'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入编号名称'
                , rules: {
                    required: true, message: '编号名称不能为空', trigger: 'blur'
                }
            }, [{
                label: '前缀'
                , name: 'code_prefx'
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入前缀'
            }, {
                label: '中缀'
                , name: 'code_midfx'
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入中缀'
            }, {
                label: '后缀'
                , name: 'code_lastfx'
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入后缀'
            }], [{
                label: '年的格式'
                , name: 'year_digit'
                , type: 'select'
                , disabled: false
                , options: [{
                    label: 'yyyy'
                    , value: 'yyyy'
                }, {
                    label: 'yy'
                    , value: 'yy'
                }]
                , value: ''
                , placeholder: '请输入年的格式（yyyy或yy）'
            }, {
                label: '月的格式'
                , name: 'month_digit'
                , type: 'select'
                , disabled: false
                , options: [{
                    label: 'mm'
                    , value: 'MM'
                }, {
                    label: 'm'
                    , value: 'M'
                }]
                , value: ''
                , placeholder: '请输入月的格式（MM或M）'
            }, {
                label: '日的格式'
                , name: 'day_digit'
                , type: 'select'
                , disabled: false
                , options: [{
                    label: 'dd'
                    , value: 'dd'
                }, {
                    label: 'd'
                    , value: 'd'
                }]
                , value: ''
                , placeholder: '请输入日的格式（dd或d）'
            }], {
                label: '流水号位数'
                , name: 'serial_digit'
                , require: true
                , type: 'number'
                , disabled: false
                , value: ''
                , placeholder: '请输入流水号位数'
                , rules: {
                    required: true, message: '流水号位数不能为空', trigger: 'blur'
                }
            }, {
                label: '模板'
                , name: 'template'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入模板'
                , rules: {
                    required: true, message: '模板不能为空', trigger: 'blur'
                }
            }]
            , op: function () {
            }
        }
    }
    , mounted: async function () { 
        const me = this; 
        me.buttons = await hcCore.getPowerButton(); 
        if (me.buttons.hasQuery) { 
            me.onQuery(); 
        } 
    } 
    , methods: { 
        handleButton: function (act_no, row) { 
            switch (act_no) { 
                case 'Add': 
                    this.showEdit('add', '新增'); 
                    break; 
                case 'Edit': 
                    this.showEdit('edit', '编辑', row); 
                    break; 
                case 'Delete': 
                    this.onDelete(row); 
                    break; 
                default: 
                    break; 
            } 
        } 
        , onQuery: function () { 
            let me = this; 
            hcCore.post({ 
                url: '/SysCode/List' 
                , param: { 
                    page: me.query.page 
                    , page_size: me.query.pageSize 
                    , code_name: me.query.codeName 
                } 
                , success: function (rsp) { 
                    me.tableData.records = rsp.data; 
                    me.tableData.total = rsp.total; 
                } 
            }) 
        } 
        , onPageChange: function (page) { 
            this.query.page = page; 
            this.onQuery(); 
        } 
        , showEdit: async function (type, title, row) { 
            let me = this; 
            me.resetParams(me.edit.fields); 
            me.edit.title = title; 
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { }); 
            if (type == 'edit') { 
                if (row) { 
                    await hcCore.post({ 
                        url: '/SysCode/Get/' + row.pkid 
                        , success: function (rsp) { 
                            me.edit.fields.forEach((item, index) => { 
                                if (Array.isArray(item) == true) {
                                    for (let i = 0; i < item.length; i++) {
                                        if (rsp.data[item[i].name]) {
                                            item[i].value = rsp.data[item[i].name];
                                        }
                                    }
                                }
                                else {
                                    if (rsp.data[item.name]) {
                                        item.value = rsp.data[item.name];
                                    }
                                }
 
                                if (item.name == 'pkid') { 
                                    item.disabled = true; 
                                } 
                            }); 
                        } 
                        , fault: '获得数据失败' 
                    }); 
                } 
                else { 
                    hcCore.showError('请选择要编辑的记录'); 
                    return; 
                } 
            } else { 
 
            } 
            me.edit.visible = true; 
        } 
        , onInsert: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysCode/Insert' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('新增成功'); 
                            me.tableData.records.push(rsp.data); 
                            me.tableData.total += 1; 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onUpdate: function () { 
            let me = this;
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysCode/Update' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('更新成功'); 
                            let index = me.tableData.records.findIndex(item => item.pkid == rsp.data.pkid); 
                            me.$set(me.tableData.records, index, rsp.data) 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onDelete: function (row) { 
            let me = this; 
            let rows = []; 
            if (row) { 
                rows = [row]; 
            } 
            else { 
                rows = me.$refs.tblRecords.selection; 
            } 
            if (rows && rows.length > 0) { 
                let tips = rows.length == 1 ? ('确认要删除【'+ rows[0].pkid +'】这条记录吗') : ('确认要删除选择【' + rows.length + '】条的记录吗？') 
                hcCore.confirm(tips, function () { 
                    let arr = rows.map(item => { return item.pkid }); 
                    let nos = arr.join(','); 
                    hcCore.post({ 
                        url: '/SysCode/Delete' 
                        , param: { 
                            pkids: nos 
                        } 
                        , success: function (rsp) { 
                            hcCore.showSuccess('删除成功'); 
                            arr.forEach(item => { 
                                let index = me.tableData.records.findIndex(node => item == node.pkid); 
                                me.tableData.records.splice(index, 1); 
                            }); 
                        } 
                    }); 
                }); 
            } 
            else { 
                hcCore.showError('请选择要删除的记录'); 
            } 
        } 
        , getParams: function () { 
            let param = {}; 
            this.edit.fields.forEach((item, index) => { 
                if (Array.isArray(item)) {
                    item.forEach((el) => {
                        param[el.name] = el.value;
                    });
                }
                else {
                    param[item.name] = item.value;
                }
            }); 
            return param; 
        } 
        , resetParams: function (obj) {
            obj.forEach(item => {
                Array.isArray(item) && this.resetParams(item);
                item.value = '';
                item.disabled = false;
            });
        } 
        , recordIndex: function (index) { 
            return (this.query.page - 1) * this.query.pageSize + index + 1; 
        } 
    } 
});